<?php

namespace App\Http\Controllers\OnlineTeam;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Service\OnlineTeam\TeamService;
use Session;
use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;

class TeamController extends Controller
{

    /**
     * @return $this
     * 客服列表
     */
    public function getOnlineTeamList()
    {
        $team = new TeamService();
        $data = $team->getOnlineTeamList();
        if($data['error_code'] == 0)
        {
            $data = $data['data'];
        }else{
            $data = [];
        }
        return view('OnlineTeam.list')->with('data',$data);
        
    }

    /**
     * @param Request $request
     * @return bool|mixed|null|string
     * 修改客服账号信息
     */
    public function onlineTeamUpdateUserInfo(Request $request)
    {
        $where = [];
        $where = $request->all();
        $where['token'] = Session::get('MEMBER_TOKEN');

        $team = new TeamService();
        $data = $team->onlineTeamUpdateUserInfo($where);

        return $data;
    }


    /**
     * 更新客服状态
     */
    public function OnlineTeamUpdate(Request $request)
    {
        $where = $request->all();
        $team = new TeamService();
        $where['token'] = Session::get('MEMBER_TOKEN');
        $data = $team->OnlineTeamUpdate($where);
        return $data;
    }

    /**
     * @param Request $request
     * 账户删除
     */
    public function onlineTeamDelete(Request $request)
    {
        $where = [];
        $where = $request->all();
        $team = new TeamService();
        $where['token'] = Session::get('MEMBER_TOKEN');
        $data = $team->OnlineTeamDelete($where);
        return $data;
    }

    /**
     * 添加客服
     */
    public function onlineTeamAdd(Request $request)
    {
        $where = [];
        $where = $request->except('_token');
        $team = new TeamService();
        $where['token'] = Session::get('MEMBER_TOKEN');
        $data = $team->OnlineTeamAdd($where);

        if($data['error_code'] == 0)
        {
            echo '<script>location.href="/onlineteam";alert("添加成功！");</script>';
        }else{
            echo "<script>alert('{$data['error_message']}');</script>";
        }
    }

    /**
     * 在线客服数据列表
     */
    public function getOnlineTeamDataList(Request $request)
    {
        $where = [];

        if($request->has('start_date'))
        {
            $where['start_date'] = $request->input('start_date');
        }


        if($request->has('end_date'))
        {
            $where['end_date'] = $request->input('end_date');
        }

        $team = new TeamService();
        $where['token'] = Session::get('MEMBER_TOKEN');
        $data = $team->getOnlineTeamDataList($where);

//        dd($data);
        if($data['error_code'] == 0)
        {
            $data = $data['data'];
        }else{
            $data = [];
        }
//
//        dd($data);
        return view('OnlineTeam.data')->with('data',$data);

    }

    /**
     * @param Request $request
     *客服数据详情列表
     */
    public function getOnlineTeamInfo(Request $request)
    {
        $where = [];

        $where['start_date'] = '';
        $where['end_date'] = '';
        $where['username'] = '';

        if($request->has('page'))
        {
            $where['page_no'] = $request->input('page');
        }

        if($request->has('start_date'))
        {
            $where['start_date'] = $request->input('start_date');
        }

        if($request->has('end_date'))
        {
            $where['end_date'] = $request->input('end_date');
        }

        if($request->has('username'))
        {
            $where['username'] = $request->input('username');
        }

        if($request->has('audit'))
        {
            $where['audit'] = $request->input('audit');
        }

        if($request->has('qq'))
        {
            $where['qq'] = $request->input('qq');
        }else{
            $where['qq'] = Session::get('ONLINE_QQ');
        }

        $where['token'] = Session::get('MEMBER_TOKEN');

//        dd($where);
        $team = new TeamService();
        $data = $team->getOnlineTeamInfo($where);
        if($data['error_code'] == 0)
        {
            $data = $data['data'];
        }else{
            $data = [];
        }

        $res = [];


        $list = $data['list'];
        $total = $data['count'];

        $res['num'] = $data['num'];
        $res['ip_num'] = $data['ip'];
        $res['turnover_ratio'] = $data['turnover_ratio'];
        $res['money'] = $data['money'];
        $res['total_count'] = $data['total_count'];



        $perPage = 50;
        $items = array_slice($list,0);
        $arr =  new LengthAwarePaginator($items,$total,$perPage);

        $arr->setPath('/onlineTeamInfo');
        return view('OnlineTeam.info')->with('data',$arr)->with('qq',$where['qq'])->with('total',$total)->with('res',$res)->withInput($request->flashOnly('start_date','end_date','username','audit'));

    }

    /**
     * @param Request $request
     * 管理员提交审核
     */
    public function submitAudit(Request $request)
    {
        $where = [];
        $where['id'] = trim($request->input('id'),',');
        $team = new TeamService();
        $data = $team->submitAudit($where);
        return $data;
    }

    /**
     * @param Request $request
     * 客服提交审核
     */
    public function AddAudit(Request $request)
    {
        $where = [];
        $where = $request->except('_token');
        $where['token'] = Session::get('MEMBER_TOKEN');
        $team = new TeamService();
        $data = $team->AddAudit($where);

        if($data['error_code'] == 0)
        {
            echo "<script>location.href='/onlineTeamInfo';alert('提交成功！');</script>";
        }else{
            echo "<script>alert('{$data['error_message']}');</script>";
        }
    }




    //  客服信息删除
    public function AuditDelete(Request $request)
    {
        $where = [];
        $where = $request->all();

        $where['token'] = Session::get('MEMBER_TOKEN');

        $team = new TeamService();
        $data = $team->AuditDelete($where);

        return $data;

    }





}
